<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Friend extends CI_Controller {
    
    function __construct()
    {                     
        parent:: __construct();
        $this->load->model('mfriend');
    }
    
    public function sendRequest() {
         if($this->session->userdata('username')) {
            if($_GET['action'] == 'send') {
                
                $adder = $_GET['id2'];
                $addee = $_GET['id'];
                if($adder == $this->session->userdata('account_id') AND $addee != $this->session->userdata('account_id')) {
                    $send = $this->mfriend->sndRequest($adder,$addee);
                     
                    switch($send){
                        case 1:
                            redirect(base_url().'account/profile?id='.$addee,'refresh');
                            break;
                        case 2:
                            redirect(base_url().'account/profile?id='.$addee.'&err=1','refresh');
                            break;
                        case 3:
                            redirect(base_url().'account/profile?id='.$addee.'&err=2','refresh');
                    }
                } else if($adder != $this->session->userdata('account_id')) {
                    redirect(base_url().'account/profile?id='.$addee.'&err=3','refresh');
                } else if($adder == $this->session->userdata('account_id') AND $addee == $this->session->userdata('account_id')) {
                    redirect(base_url().'account/profile?id='.$addee.'&err=4','refresh');
                }
            }
         }
         
    }
 
    public function approveRequest() {
        if($this->session->userdata('username')) {
            if($_GET['action'] == 'accept'){
                $adder = $_GET['id'];
                $addee = $_GET['id2'];
                               
                if($adder != $this->session->userdata('account_id') AND $addee == $this->session->userdata('account_id')){
                    $approve = $this->mfriend->apprvRequest($adder,$addee);
                
                    switch($approve){
                        case 1:
                            redirect(base_url().'home/friendRequest','refresh');
                            break;
                    }
                }
            }
        }
    }
    
    public function rejectRequest() {
        if($this->session->userdata('username')) {
            if($_GET['action'] == 'reject'){
                $adder = $_GET['id'];
                $addee = $_GET['id2'];
                               
                if($adder != $this->session->userdata('account_id') AND $addee == $this->session->userdata('account_id')){
                    $reject = $this->mfriend->rjectRequest($adder,$addee);
                
                    switch($reject){
                        case 1:
                            redirect(base_url().'home/friendRequest','refresh');
                            break;
                    }
                }
            }
        }
    }

}